var eventsMgr = require('eventsMgr');
var consts = require('consts');
var Level = require('Level');
var metaCfg = require('metaCfg');
var nodeMgr = require('nodeMgr');
var HashMap = require('HashMap');
var LevelMgr = function () {
    this.curLevel = null;
    this.curLevelNode = null;
    this.curLevelData = undefined;
}
var pro = LevelMgr.prototype;

pro.initLevel = function(){
    var self = this;
}



/**
 * 载入关卡
 * @param data
 * @private
 */
pro._loadNewLevel = function(data){
    // 10	载入关卡1	1000	1													载入关卡事件
    var self = this;
    self.curLevel = data.target;
    if (self.hasLevel(self.curLevel)){
        var levelData = self.getLevelData(self.curLevel);
        self.curLevelData = levelData;
        if (!!levelData.pass){
            self.shareDatas.put(levelData.pass,0);//载入关卡初始数值
        }
        cc.log('载入关卡:',levelData);
        cc.loader.loadRes(levelData.res,cc.Prefab,function (err,res) {
            if (!err && res){
                self._addLevel(res);
            }else{
                cc.warn('关卡预制体加载失败:',levelData.res);
            }
        })
    }else{
        cc.warn('关卡数据不存在:id=',self.curLevel);
    }
}
pro.loadLevel = function(levelPath,callback){
    var self = this;
    cc.loader.loadRes(levelPath,cc.Prefab,function (err,res) {
        if (!err && res){
            self._addLevel(res);
            callback(null);
        }else{
            cc.warn('关卡预制体加载失败:',levelPath);
            callback({msg:'加载失败'});
        }
    })
}
/**
 * 加入到场景
 * @param res
 * @private
 */
pro._addLevel = function(res){
    var self = this;
    var levelNode = cc.instantiate(res);
    self.curLevel = levelNode.getComponent(Level);
    // level.setLevelData(self.curLevelData);
    self.curLevelNode = levelNode;
    nodeMgr.addNode(levelNode);
}
pro.passLevel = function(){
    var self = this;
    if (!!self.curLevelNode){
        self.curLevelNode.destroy();
    }
}

module.exports = new LevelMgr();